package xyz.kongbai121.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import xyz.kongbai121.common.utils.ShiroUtils;
import org.apache.ibatis.reflection.MetaObject;

import java.util.Date;

/**
 * @author zbn
 */
public class MyMetaObjectHandler implements MetaObjectHandler {
    private static final String CREATE_TIME = "createTime";
    private static final String UPDATE_TIME = "updateTime";
    private static final String UPDATE_ID = "updateId";

    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName(CREATE_TIME, new Date(), metaObject);
        this.setFieldValByName(UPDATE_TIME, new Date(), metaObject);
        this.setFieldValByName(UPDATE_ID, ShiroUtils.getUserId(), metaObject);
    }

    // 更新时的填充策略
    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName(UPDATE_TIME, new Date(), metaObject);
        this.setFieldValByName(UPDATE_ID, ShiroUtils.getUserId(), metaObject);
    }
}